package org.wu.framework.saas.share.platform.interceptor;

import org.springframework.stereotype.Component;
import org.wu.framework.lazy.orm.database.sql.expand.database.persistence.method.interceptor.TenantIdSqlInterceptor;
import org.wu.framework.lazy.orm.database.sql.expand.database.persistence.method.interceptor.tenant.LazyTenantLineHandler;
import org.wu.framework.saas.share.platform.prop.SaasShareProp;
import org.wu.framework.saas.share.platform.toolkit.SaasShareContextHolder;

@Component
public class SaasShareTenantIdSqlInterceptor extends TenantIdSqlInterceptor {


    public SaasShareTenantIdSqlInterceptor(SaasShareProp saasShareProp) {
        super(new SaasShareLazyTenantLineHandler(saasShareProp.getShareColumnName()));
    }

}

record SaasShareLazyTenantLineHandler(String tenantIdColumn) implements LazyTenantLineHandler {

    /**
     * 获取租户 ID 值表达式，只支持单个 ID 值
     * <p>
     *
     * @return 租户 ID 值表达式
     */
    @Override
    public String getTenantId() {
        // 上下文获取租户ID
        return SaasShareContextHolder.peek();
    }

    /**
     * 获取租户字段名
     * <p>
     * 默认字段名叫: tenant_id
     *
     * @return 租户字段名
     */
    @Override
    public String tenantIdColumn() {
        return tenantIdColumn;
    }
}
